# Contributor: <xmingske@gmail.com>
# Contributor: Sascha Paunovic <azarus@posteo.net>
# Contributor: Patrycja Rosa <alpine@ptrcnull.me>
# Maintainer: Patrycja Rosa <alpine@ptrcnull.me>
pkgname=minetest
pkgver=5.8.0
pkgrel=1
pkgdesc="An infinite-world block sandbox game and a game engine"
url="https://www.minetest.net/"
# ppc64le, riscv64, loongarch64 blocked by luajit
arch="all !ppc64le !riscv64 !loongarch64"
license="LGPL-2.1-or-later AND CC-BY-SA-3.0"
pkgusers="minetest"
pkggroups="minetest"
install="$pkgname-server.pre-install"
depends="$pkgname-common"
makedepends="
	bzip2-dev
	cmake
	curl-dev
	freetype-dev
	gettext-dev
	hiredis-dev
	libjpeg-turbo-dev
	libogg-dev
	libpng-dev
	libvorbis-dev
	libxi-dev
	luajit-dev
	mesa-dev
	openal-soft-dev
	openssl-dev>3
	samurai
	sqlite-dev
	zstd-dev
	"
subpackages="$pkgname-doc
	$pkgname-lang
	$pkgname-common::noarch
	$pkgname-server
	$pkgname-server-openrc:openrc
	"
_irrlichtver="1.9.0mt13"
source="https://github.com/minetest/minetest/archive/refs/tags/$pkgver/minetest-$pkgver.tar.gz
	https://github.com/minetest/irrlicht/archive/refs/tags/$_irrlichtver/irrlichtmt-$_irrlichtver.tar.gz
	minetest-server.confd
	minetest-server.initd
	"

prepare() {
	default_prepare

	mv "$srcdir"/irrlicht-$_irrlichtver "$srcdir"/minetest-$pkgver/lib/irrlichtmt
}

build() {
	cmake -G Ninja -B build_client \
		-DCMAKE_BUILD_TYPE=Release \
		-DCUSTOM_BINDIR=/usr/bin \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCUSTOM_DOCDIR="/usr/share/doc/$pkgname" \
		-DCUSTOM_SHAREDIR="/usr/share/$pkgname" \
		-DBUILD_CLIENT=1 \
		-DBUILD_SERVER=0 \
		-DENABLE_REDIS=0 \
		-DENABLE_CURL=1 \
		-DRUN_IN_PLACE=0
	cmake --build build_client

	cmake -G Ninja -B build_server \
		-DCMAKE_BUILD_TYPE=Release \
		-DCUSTOM_BINDIR=/usr/bin \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCUSTOM_DOCDIR="/usr/share/doc/minetest" \
		-DCUSTOM_SHAREDIR="/usr/share/minetest" \
		-DBUILD_CLIENT=0 \
		-DBUILD_SERVER=1 \
		-DENABLE_REDIS=1 \
		-DENABLE_CURL=1 \
		-DRUN_IN_PLACE=0
	cmake --build build_server
}

package() {
	DESTDIR="$pkgdir" cmake --install build_client
}

common() {
	pkgdesc="Minetest files used by both client & server"
	depends=""

	install -o minetest -g minetest -d "$subpkgdir"/var/lib/minetest
	amove \
		usr/share/minetest/builtin
}

server() {
	pkgdesc="Minetest server"
	depends="$pkgname-common"

	cd "$builddir"
	install -Dm644 minetest.conf.example \
		"$subpkgdir"/etc/minetest/minetest.conf
	install -Dm755 bin/minetestserver \
		-t "$subpkgdir"/usr/bin
}

openrc() {
	pkgdesc="Minetest server (OpenRC init scripts)"
	install_if="$pkgname-server=$pkgver-r$pkgrel openrc"

	install -Dm755 "$srcdir"/minetest-server.initd \
		"$subpkgdir"/etc/init.d/minetest-server
	install -Dm644 "$srcdir"/minetest-server.confd \
		"$subpkgdir"/etc/conf.d/minetest-server
}

sha512sums="
0b376ecc051032ccb542f2f76e2b1f5c351b32f1c211027825b1538095f44cb1447cd0976cb85a4cde4ecb53873bcb9f3778799271ed65b038a031dc137c2aba  minetest-5.8.0.tar.gz
5226d78eaacf8b56eafbbb6359bfb6137f9d6a555a252224e33d39c0d219fcb7d0d29fd3828961eee37d55940f3aa6e767fea31c96fb772dd5eca3889f5ac16e  irrlichtmt-1.9.0mt13.tar.gz
7bca17dc0bd59db9c07c160677498aa09297026104e922bf93b7629c274d0aebd226af60c5e82f49ffe3cc5c1e4448e3798b6c2a44144fc6eca4d3aaed3384bb  minetest-server.confd
c27aea87468538b48b5e0a66fa5cdde4fc67f3c00a0e57f359a2f044a31c5617d1b5f3dec72e015537ee3bf126bd1073878c0f4447de67db9be2c4cf21416dd7  minetest-server.initd
"
